namespace egret.socketio {

    export class Url {
        static PATTERN_HTTP: RegExp = /^http|ws$/
        static PATTERN_HTTPS: RegExp = /^(http|ws)s$/

        private source: string;
        private uri: Uri;
        public protocol: string;
        public path;
        public host;
        public port;


        constructor(uri: Uri | string) {
            if (typeof uri === 'string') {
                uri = new Uri(uri);
            }
            this.uri = uri;
            this.protocol = uri.protocol;
            let path = uri.path;
            if (typeof path === 'undefined' || path.length == 0) {
                path = '/';
            }
            this.path = path;
            this.host = uri.host;
            let port = uri.port;
            if (typeof port === 'undefined') {
                if (Url.PATTERN_HTTP.test(this.protocol)) {
                    port = 80;
                } else {
                    port = 443;
                }
            }

        }

        public toString(): string {
            let url: string;
            if (this.protocol === null || this.protocol.match(/^https?|wss?$/g)) {
                url = 'https';
            }

            let path = this.path;
            if (typeof path === 'undefined' || path.length == 0) {
                path = '/';
            }

            if (this.uri.userInfo !== null) {
                url += this.uri.userInfo + "@"
            }

            url += this.uri.host;

            let port = this.port;
            if (typeof port === 'undefined') {
                if (Url.PATTERN_HTTP.test(this.protocol)) {
                    port = 80;
                } else {
                    port = 443;
                }
            }
            url += ':' + port.toString();
            url += path;
            if (this.uri.query) {
                url += '?' + this.uri.query;
            }
            if (this.uri.anchor) {
                if (this.uri.anchor.indexOf('#') !== 0) {
                    url += '#';
                }
                url += this.uri.anchor;
            }

            return url;
        }

        static parse(uri: Uri | string): Url {

            return new Url(uri);
        }

        public static extractId(uri: Uri | string): string {
            if (typeof uri === 'string') {
                uri = new Uri(uri);
            }

            let protocol = uri.protocol;
            let port = uri.port;
            if (!port) {
                if (this.PATTERN_HTTP.test(protocol)) {
                    port = 80;
                } else if (this.PATTERN_HTTPS.test(protocol)) {
                    port = 443;
                }
            }

            return protocol + '://' + uri.host + ':' + port.toString();
        }

        public static secure(uri: Uri | string): boolean {
            if (typeof uri === 'string') {
                uri = new Uri(uri);
            }

            return this.PATTERN_HTTPS.test(uri.protocol);
        }
    }
}